package com.linkvin.ec.hcs.service.mdm.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.linkvin.ec.hcs.dao.mapper.mdm.WarehouseMapper;
import com.linkvin.ec.hcs.dao.model.mdm.Warehouse;
import com.linkvin.ec.hcs.dao.model.mdm.bo.WarehouseBo;
import com.linkvin.ec.hcs.service.mdm.WarehouseService;
import com.linkvin.ec.hcs.service.util.BaseException;
import com.linkvin.ec.hcs.service.util.enums.WarehouseTypeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Override
    public Page<WarehouseBo> queryPageWarehouse(Page<WarehouseBo> page) {
        List<WarehouseBo> listDepartBo = warehouseMapper.queryPageWarehouse(page,buildCondition(page.getCondition()));
        page.setRecords(listDepartBo);
        return page;
    }

    @Override
    public WarehouseBo addWarehouse(WarehouseBo warehouseBO) {

        //非空校验
        if (Strings.isNullOrEmpty(warehouseBO.getCode().trim())) throw new BaseException("仓库编码不能为空");
        if (Strings.isNullOrEmpty(warehouseBO.getName().trim())) throw new BaseException("仓库名称不能为空");
        if (Objects.equals(warehouseBO.getOrgId(),null)) throw new BaseException("所属组织不能为空");

        //判断是否重复
        EntityWrapper<Warehouse> ew = new EntityWrapper<>();
        ew.where("code={0}",warehouseBO.getCode().trim()).or("name={0}",warehouseBO.getName().trim());
        int count = warehouseMapper.selectCount(ew);
        if(count > 0){
            throw new BaseException("仓库编码或者编码重复");
        }

        Warehouse warehouse = new Warehouse();
        BeanUtils.copyProperties(warehouseBO, warehouse);
        count = warehouseMapper.insert(warehouse);

        if(count > 0){
            warehouseBO = getWarehouseBO(warehouseBO.getCode());
        }

        return  warehouseBO;
    }

    @Override
    public WarehouseBo updateWarehouse(WarehouseBo warehouseBO) {

        //非空校验
        if (Strings.isNullOrEmpty(warehouseBO.getCode().trim())) throw new BaseException("仓库编码不能为空");
        if (Strings.isNullOrEmpty(warehouseBO.getName().trim())) throw new BaseException("仓库名称不能为空");
        if (Objects.equals(warehouseBO.getOrgId(),null)) throw new BaseException("所属组织不能为空");

        //判断是否重复
        EntityWrapper<Warehouse> ew = new EntityWrapper<>();
        ew.where("id != {0}",warehouseBO.getId()).andNew("code={0}",warehouseBO.getCode().trim()).or("name={0}",warehouseBO.getName().trim());
        int count = warehouseMapper.selectCount(ew);
        if(count > 0){
            throw new BaseException("仓库编码或者编码重复");
        }

        Warehouse warehouse = new Warehouse();
        BeanUtils.copyProperties(warehouseBO, warehouse);
        count = warehouseMapper.updateById(warehouse);

        if(count > 0){
            warehouseBO = getWarehouseBO(warehouseBO.getCode());
        }
        return  warehouseBO;
    }

    @Override
    public int deleteWarehouse(List<Integer> warehouseIds) {
        //TODO 需要判断是否已经使用
        return warehouseMapper.deleteBatchIds(warehouseIds);
    }


    @Override
    public List<WarehouseBo> listAllWarehouse() {
        List<WarehouseBo> listWarehouseBo = warehouseMapper.selectAllWarehouse();
        return listWarehouseBo;
    }

    private WarehouseBo getWarehouseBO(String code){
        WarehouseBo warehouseBO = new WarehouseBo();
        List<WarehouseBo> listWarehouseBo= warehouseMapper.selectWarehouse(code);
        if(!CollectionUtils.isEmpty(listWarehouseBo)){
            warehouseBO = listWarehouseBo.get(0);
        }
        return warehouseBO;
    }

    private EntityWrapper<WarehouseBo> buildCondition(Map<String,Object> condition){
        EntityWrapper<WarehouseBo> ew = new EntityWrapper<>();

        if(!Objects.equals(condition,null)) {
            for (Map.Entry<String, Object> entry : condition.entrySet()) {
                if (entry.getKey().equals("code")) {
                    ew.eq("code", entry.getValue().toString());
                }
                if (entry.getKey().equals("name")) {
                    ew.like("name", entry.getValue().toString());
                }
                if (entry.getKey().equals("orgId")) {
                    ew.eq("org_id", entry.getValue().toString());
                }
                if (entry.getKey().equals("userId")) {
                    ew.eq("user_id", entry.getValue().toString());
                }
                if (entry.getKey().equals("orgName")) {
                    ew.eq("org_name", entry.getValue().toString());
                }
                if (entry.getKey().equals("userName")) {
                    ew.eq("user_name", entry.getValue().toString());
                }
            }
        }
        return ew;
    }
}
